#coding:utf8
from stanfordcorenlp import StanfordCoreNLP
from nltk.tree import Tree
import base64

jar_path = r'C:/D/tools/java/jdk1.8.0_121/lib/stanford-corenlp-4.2.0/'
src_path = '../static/structure/%s'
png_path = 'C:/E/work_myself/python_study/my-django/lehexiaoyuanzahuopu/fanyi/static/structure/%s'
def generateImg(imgName,sentence):
  imgName += r'.png'
  with StanfordCoreNLP(jar_path,lang='en') as nlp:
    nlp_parse = nlp.parse(sentence)
    #Tree.fromstring(nlp_parse).draw()
    Tree.fromstring(nlp_parse)._repr_png_()
    png = open(png_path % imgName,'wb')
    png.write(base64.b64decode(Tree.fromstring(nlp_parse)._repr_png_()))
    png.close()
    nlp.close()
  return src_path % imgName

if __name__ == '__main__':
  generateImg('haha','''Archegos’ forced liquidation of several of its positions triggered a fire sale of a number of U.S. media stocks last week.''')